<?php
namespace app\shops\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;


/**
 * 公告管理
 */
class Notice extends Base
{
    /**
     * @throws
     * 公告列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // notice列表
        $notices = Db("notice")->where("shop_id",$this->shop_id)->order('sort desc, id asc')->paginate($pageSize);

        // 查询所有门店信息
        $store_list = Db("store")->where("shop_id",$this->shop_id)->select();

        // 整理门店信息
        if ($store_list){
            foreach ($store_list as $k=>$v){
                $newStore[$v['store_id']] = $v['store_name'];
            }
        }

        $notice_list = [];
        //数据处理
        if($notices){
            foreach ($notices as $k => $v){
                $notice_list[$k] = $v;
                if($v['store_id'] >0){
                    $notice_list[$k]['store_name'] = $newStore[$v['store_id']];
                }else{
                    $notice_list[$k]['store_name'] = "所有门店";
                }

            }
        }
        $this->assign([
            'total'          => $notices->total(),
            'page'           => $notices->render(),
            'notice_list'    =>$notice_list
        ]);

        return $this->fetch();
    }


    /**
     * 添加公告
     */
    public function add()
    {
        if(request()->isAjax()){

            $data = [
                'shop_id'           => $this->shop_id,
                'store_id'          =>input('store_id'),
                'title'             => input('title'),
                'sort'              => input('sort'),
                'is_show'           => input('is_show'),
                'create_time'       => time(),
            ];

            $ret =Db('notice')->insert($data);

            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }

        //拿到门店列表
        $store_list = Db('store')->field('store_name,store_id')->where('shop_id',$this->shop_id)->select();

        $this->assign('store_list',$store_list);

        return $this->fetch();
    }


    /**
     * @throws
     * 编辑公告
     */
    public function edit()
    {

        $id = input('id');
        // 更新数据
        if(request()->isAjax()){

            input('id')           == null || $data['id']           = input('id');
            input('title')        == null || $data['title']        = input('title');
            input('store_id')     == null || $data['store_id']     = input('store_id');
            input('sort')         == null || $data['sort']         = input('sort');
            input('is_show')      == null || $data['is_show']      = input('is_show');


            $res = Db("notice")
                ->where('shop_id',$this->shop_id)
                ->where('id',$id)
                ->update($data);

            if($res===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");

        }

        //拿到门店列表
        $store_list = Db('store')->field('store_name,store_id')->where('shop_id',$this->shop_id)->select();

        $this->assign('store_list',$store_list);

        //拿到该公告相关的信息
        $notice_info = Db("notice")->field('id, title, sort, is_show,store_id')
            ->where('shop_id',$this->shop_id)->where('id',$id)->find();

        $this->assign('notice_info',$notice_info);

        return $this->fetch();
    }

    /**
     * 排序
     */
    public function sort(){

        $id = input('id');
        if (request()->isAjax()){

            $sort        = input('sort');

            // 更新排序顺序
            $res = Db('notice')->where('shop_id', $this->shop_id)->where('id', $id)->update(['sort' => $sort]);

            // 返回结果
            if($res){
                $this->success("排序成功");
            }else{
                $this->error("网络异常");
            }
        }
    }

    
}
